package
components {

import core.AGameObject;
import core.Facade;
import core.Facade;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;

import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;


public class MapScroller extends AGameObject {
    private var _offset_x:int;
    private var _offset_y:int;
    private var _point:Point;
    private var _oldPoint:Point;
    private var _zeroPoint:Point;
    private var _rect:Rectangle;
    private var _backBitmap:Bitmap;
    private var _bitmapData:BitmapData;
    private var _backBitmapData:BitmapData;

    private var _maxHeight:int;
    private var _maxWidth:int;

    private var _clicked:Boolean;
    private var _clickX:int;
    private var _clickY:int;
    private var _clickOldX:int;
    private var _clickOldY:int;

    private var _delta_x:int;
    private var _delta_y:int;

    private var _w:int;
    private var _h:int;


    public function MapScroller(bitmap:Bitmap) {
        super();
        _clicked = false;
        mouseChildren = false;
        this._w = 640;
        this._h = 605;


        _offset_x = _offset_y = 0;
        _zeroPoint = new Point(0, 0);
        _oldPoint = new Point(_offset_x, _offset_y);
        _point = new Point(_offset_x, _offset_y);
        _rect = new Rectangle(_offset_x, _offset_y, this._w, this._h);

        _bitmapData = bitmap.bitmapData;
        _backBitmapData = new BitmapData(this._w, this._h);
        _backBitmap = new Bitmap(_backBitmapData);

        _maxWidth = bitmap.bitmapData.width;
        _maxHeight = bitmap.bitmapData.height;

        Facade.getInstance().MAXWIDTH = _maxWidth;
        Facade.getInstance().MAXHEIGHT = _maxHeight;

        _delta_x = Facade.WIDTH - this._w;
        _delta_y = Facade.HEIGHT - this._h;

        _backBitmapData.copyPixels(_bitmapData, _rect, _zeroPoint);

        addChild(_backBitmap);
        addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
        addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
        Facade.getInstance().stage.addEventListener(Event.MOUSE_LEAVE, onMouseUp);
    }

    //this function recompute size of component
    public function recalc():void{
        _delta_x = Facade.WIDTH - this._w;
        _delta_y = Facade.HEIGHT - this._h;
    }
    private function onMouseDown(event:MouseEvent):void {
        _clicked = true;
        _clickX = event.stageX;
        _clickY = event.stageY;
        _clickOldX = _offset_x;
        _clickOldY = _offset_y;
        addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
    }

    private function onMouseUp(event:Event):void {
        _clicked = false;
        removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
    }

    private function onMouseMove(event:MouseEvent):void {
        if (event.target != event.currentTarget) return;
        _offset_x = _clickOldX + _clickX - event.stageX;
        _offset_y = _clickOldY + _clickY - event.stageY;

        if (_offset_x < 0) _offset_x = 0;
        if (_offset_y < 0) _offset_y = 0;

        if (_offset_x > (_maxWidth - Facade.WIDTH )) _offset_x = _maxWidth - Facade.WIDTH ;
        if (_offset_y > (_maxHeight - Facade.HEIGHT )) _offset_y = (_maxHeight - Facade.HEIGHT );

        if (_offset_x < 0) _offset_x = 0;
        if (_offset_y < 0) _offset_y = 0;

        _point.x = _offset_x;
        _point.y = _offset_y;

    }

    override public function update():void {
        super.update();

        if ((_oldPoint.x != _point.x) || (_oldPoint.y != _point.y)) {
            _rect.x = _offset_x;
            _rect.y = _offset_y;
            _backBitmapData.copyPixels(_bitmapData, _rect, _zeroPoint);
            _oldPoint.x = _point.x;
            _oldPoint.y = _point.y;
        }
    }

    public function get offset_x():int {
        return -_offset_x;
    }

    public function get offset_y():int {
        return -_offset_y;
    }
}
}